import pygame
from pygame.locals import *
from sys import exit
import os
import Menu_principal


class Menu:
    def __init__(self):
        self.player1 = 1
        self.player2 = 3
        self.player1_ativo = True
        self.player2_ativo = True
        self.iniciados = []
        #informacoes do menu
        self.width, self.height = 798,672
        self.cor = (8,91,47)
        
        
        self.lady_menu = pygame.image.load("img"+ os.sep +"lady"+ os.sep +"menu_sel.png")
        self.lady_size = self.lady_menu.get_size()
        self.lady_pos = (self.width/5,self.height/4)

        self.jet_menu = pygame.image.load("img"+ os.sep +"jet"+ os.sep +"menu_sel.png")
        self.jet_size = self.jet_menu.get_size()
        self.jet_pos = (self.lady_pos[0]+self.lady_size[0]+10,self.height/4)

        self.white_menu = pygame.image.load("img"+ os.sep +"white"+ os.sep +"menu_sel.png")
        self.white_size = self.white_menu.get_size()
        self.white_pos = (self.jet_pos[0]+self.jet_size[0]+10,self.height/4)

        
    def mudaImagem(self):
        if self.player1 == 1 and self.player2 == 3:
            self.lady_menu = pygame.image.load("img"+ os.sep +"lady"+ os.sep +"menu_sel_player1.png")
            self.jet_menu = pygame.image.load("img"+ os.sep +"jet"+ os.sep +"menu_sel.png")
            self.white_menu = pygame.image.load("img"+ os.sep +"white"+ os.sep +"menu_sel_player2.png")
        elif self.player1 == 1 and self.player2 == 2:
            self.lady_menu = pygame.image.load("img"+ os.sep +"lady"+ os.sep +"menu_sel_player1.png")
            self.jet_menu = pygame.image.load("img"+ os.sep +"jet"+ os.sep +"menu_sel_player2.png")
            self.white_menu = pygame.image.load("img"+ os.sep +"white"+ os.sep +"menu_sel.png")
        elif self.player1 == 2 and self.player2 == 1:
            self.lady_menu = pygame.image.load("img"+ os.sep +"lady"+ os.sep +"menu_sel_player2.png")
            self.jet_menu = pygame.image.load("img"+ os.sep +"jet"+ os.sep + "menu_sel_player1.png") 
            self.white_menu = pygame.image.load("img"+ os.sep +"white"+ os.sep +"menu_sel.png")
        elif self.player1 == 2 and self.player2 == 3:
            self.lady_menu = pygame.image.load("img"+ os.sep +"lady"+ os.sep +"menu_sel.png")
            self.jet_menu = pygame.image.load("img"+ os.sep +"jet"+ os.sep + "menu_sel_player1.png") 
            self.white_menu = pygame.image.load("img"+ os.sep +"white"+ os.sep +"menu_sel_player2.png")
        elif self.player1 == 3 and self.player2 == 1:
            self.lady_menu = pygame.image.load("img"+ os.sep +"lady"+ os.sep +"menu_sel_player2.png")
            self.jet_menu = pygame.image.load("img"+ os.sep +"jet"+ os.sep +"menu_sel.png")
            self.white_menu = pygame.image.load("img"+ os.sep +"white"+ os.sep +"menu_sel_player1.png")
        elif self.player1 == 3 and self.player2 == 2:
            self.lady_menu = pygame.image.load("img"+ os.sep +"lady"+ os.sep +"menu_sel.png")
            self.jet_menu = pygame.image.load("img"+ os.sep +"jet"+ os.sep +"menu_sel_player2.png")
            self.white_menu = pygame.image.load("img"+ os.sep +"white"+ os.sep +"menu_sel_player1.png")
        
 
    def executaMenu(self):
        pygame.init()
        tela = pygame.display.set_mode((self.width,self.height), 0, 32)
        pygame.display.set_caption("Menu")
        background = pygame.image.load("img"+ os.sep +"cenario"+ os.sep +"MegaBomb.jpg").convert()
        font = pygame.font.Font("fontes" + os.sep + "chonker.ttf", 20, bold = False)
        
        while True:
            tela.fill(self.cor)
            for event in pygame.event.get():
                if event.type == QUIT:
                    exit(0)

            botao_pressionado = pygame.key.get_pressed()

            if self.player1 == 1:
                selecao_1 = "lady"
            elif self.player1 == 2:
                selecao_1 = "jet"
            elif self.player1 == 3:
                selecao_1 = "white"

            if self.player2 == 1:
                selecao_2 = "lady"
            elif self.player2 == 2:
                selecao_2 = "jet"
            elif self.player2 == 3:
                selecao_2 = "white"
            display_p1 = font.render("Player1: "+selecao_1.capitalize() ,False,(0,0,255))
            display_p2 = font.render("Player2: "+selecao_2.capitalize() ,False,(255,0,0))
            bonus_p1 = pygame.image.load("img"+ os.sep +selecao_1+ os.sep +"extra.png").convert_alpha()
            bonus_p2 = pygame.image.load("img"+ os.sep +selecao_2+ os.sep +"extra.png").convert_alpha()

            if botao_pressionado[K_ESCAPE]:
                Menu_principal.main()
            if self.player1_ativo:
                if botao_pressionado[K_RIGHT]:
                    if self.player1 + 1 > 3:
                        pass
                    elif (self.player1 + 1 < self.player2) or (self.player1 > self.player2 and self.player1 +1 <= 3):
                        self.player1 += 1
                    elif self.player1 + 1 == self.player2 and self.player1 + 2 <= 3:
                        self.player1 += 2
                    elif self.player1 + 1 == self.player2 and self.player1 + 2 > 3:
                        pass
                    
                        
                    
                elif botao_pressionado[K_LEFT]:
                    
                    if self.player1 - 1 <= 0:
                        pass
                    elif self.player1 - 1 != self.player2:
                        self.player1 -= 1
                    elif self.player1 - 1 == self.player2 and self.player1 - 2 >= 1:
                        self.player1 -= 2
                    elif self.player1 - 1 == self.player2 and self.player1 - 2 < 1:
                        pass
                elif botao_pressionado[K_RETURN]:
                    self.iniciados.append(self.player1)
                    self.player1_ativo = False


            if self.player2_ativo:
                if botao_pressionado[K_a]:
                    
                    if self.player2 - 1 <= 0:
                        pass
                    elif (self.player2 - 1 > self.player1 or self.player2 -1 < self.player1) and self.player2 - 1 >= 1:
                        self.player2 -= 1
                    elif self.player2 - 1 == self.player1 and self.player2 - 2 >= 1:
                        self.player2 -= 2
                    elif self.player2 - 1 == self.player1 and self.player2 - 2 < 1:
                        pass


                elif botao_pressionado[K_d]:
                    if self.player2 + 1 > 3:
                        pass
                    elif self.player2 + 1 != self.player1:
                        self.player2 += 1
                    elif self.player2 + 1 == self.player1 and self.player2 + 2 <= 3:
                        self.player2 += 2
                    elif self.player2 + 1 == self.player1 and self.player2 + 2 > 3:
                        pass
                elif botao_pressionado[K_SPACE]:
                    self.iniciados.append(self.player2)
                    self.player2_ativo = False

            if len(self.iniciados) == 2:
                import menu_mapa
                proximo = menu_mapa.Fase(self.player1,self.player2)
                proximo.executa_menu()
            self.mudaImagem()
            tela.blit(background,(0,0))
            tela.blit(self.lady_menu, self.lady_pos)
            tela.blit(self.jet_menu, self.jet_pos)
            tela.blit(self.white_menu, self.white_pos)
            tela.blit(display_p1,(self.lady_pos[0],550))
            tela.blit(display_p2,(self.white_pos[0],550))
            tela.blit(bonus_p1,(self.lady_pos[0],590))
            tela.blit(bonus_p2,(self.white_pos[0],590))

            #pygame.time.delay(100)
            pygame.display.update()


            
                    
                
                
                
                


            
        

        
        
       
        
        
